<?php

declare(strict_types=1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("系统登录")
 * @Apidoc\Group("login")
 */
class Login extends Baseic
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @Apidoc\Title("系统登录")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("admin/v1.login")
     * @Apidoc\Param("username", type="string",require=true,default="admin", desc="用户名" )
     * @Apidoc\Param("password", type="string",require=true,default="admin", desc="登录密码" )
     */
    public function index($username, $password)
    {
        try {
            $adminDB = new \hangpu8\hpcms\service\Login;
            $token = (string)$adminDB->login($username, $password);
            return Json::successful(['token'=> $token],'登录成功');
        } catch (\Throwable $e) {
            list($code,$msg) = explode("\r\n",$e->getMessage());
            return Json::fail($msg,(int)$code);
        }
    }
}
